<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>

    <script src='../node_modules/react/umd/react.development.js'></script>
    <script src='../node_modules/react-dom/umd/react-dom.development.js'></script>
    <script src='../node_modules/babel-standalone/babel.min.js'></script>
    <script src='../node_modules/prop-types/prop-types.js'></script>
</head>

<body>
    <div id='root'></div>
    <script type='text/babel'>
        class MyCom extends React.Component {

            constructor(props) {
                super(props)
                console.log("this in constructor",this);

                /* 将实例方法中的this绑定为当前实例对象 */
                this.fn1 = this.fn1.bind(this)
            }

            /* 类组件中的实例方法（普通函数）中的this是undefined */
            /* 仅仅将myCom实例中的fn0函数地址作为button的事件处理函数 由window调用 但被严格模式屏蔽 打印结果是undefined */
            fn0() {
                console.log("this in fn0 = ", this);
            }

            /* this已经在constructor中绑定为当前实例 */
            fn1() {
                console.log("this in fn1 = ", this);
            }

            /* this绑定为render函数中的this(当前组件实例) */
            fn2() {
                console.log("this in fn2 = ", this);
            }

            /* 类组件中的箭头函数中的this就是当前实例 */
            fn3 = () => {
                console.log("this in fn3 = ", this);
            }

            /* 直接在render函数中带主语地进行调用：this.fn4() */
            fn4() {
                console.log("this in fn4 = ", this);
            }

            /* render方法中的this为当前组件实例=>JSX中的this也是当前实例 */
            render() {
                console.log("this in render=", this);
                return <div>
                    <button onClick={this.fn0}>fn0</button>

                    <button onClick={this.fn1}>fn1</button>

                    <button onClick={this.fn2.bind(this)}>fn2</button>

                    <button onClick={this.fn3}>fn3</button>

                    <button onClick={
                        () => {
                            this.fn4()
                        }
                    }>fn4</button>
                </div>
            }

        }
        
        ReactDOM.render(<MyCom />, document.getElementById('root'))
    </script>
</body>

</html>